package com.durian.base.utils.task;

import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner;


/**
 * 说明：TaskEngine
 * <p/>
 * 作者：fanly
 * <p/>
 * 类型：Class
 * <p/>
 * 时间：2019-12-18 14:27
 * <p/>
 * 版本：version 1.0
 */
public class TaskEngine extends TaskExecutor {

    private static volatile TaskEngine sInstance;

    @NonNull private TaskExecutor mDefaultTaskExecutor;

    private TaskEngine() {
        mDefaultTaskExecutor = new DefaultTaskExecutor();
    }

    /**
     * Returns an instance of the task executor.
     *
     * @return The singleton ArchTaskExecutor.
     */
    @NonNull
    public static TaskEngine getInstance() {
        if (sInstance != null) {
            return sInstance;
        }
        synchronized (TaskEngine.class) {
            if (sInstance == null) {
                sInstance = new TaskEngine();
            }
        }
        return sInstance;
    }

    @Override
    public void executeOnDiskIO(Runnable runnable) {
        mDefaultTaskExecutor.executeOnDiskIO(runnable);
    }

    @Override
    public void postToMainThread(Runnable runnable) {
        mDefaultTaskExecutor.postToMainThread(runnable);
    }


    @Override
    public void executeOnDiskIO(LifecycleOwner lifecycleOwner,TaskRunnable task) {
        mDefaultTaskExecutor.executeOnDiskIO(lifecycleOwner,task);
    }

    @Override
    public boolean isMainThread() {
        return mDefaultTaskExecutor.isMainThread();
    }

}
